﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LibExtend
{
    public partial class OptionFrom : Form
    {
        public event LibExtend.Events.DataExchangeHandler DataExchange;

        public Table FromTable { get; set; }
        public Table ToTable { get; set; }

        public OptionFrom()
        {
            InitializeComponent();
        }

        private void OptionFrom_Load(object sender, EventArgs e)
        {
            rdoGrp.Properties.Items[0].Enabled = AllowMove();
            rdoGrp.Properties.Items[1].Enabled = AllowMerge();

            rdoGrp.EditValue = (rdoGrp.Properties.Items[0].Enabled) ? 1 : 2;
            RdoEnable();

            GetData();

            if (!rdoGrp.Properties.Items[0].Enabled && !rdoGrp.Properties.Items[1].Enabled)
            {
                this.Close();
                return;
            }

            lbl1.Text = FromTable.TenBan;
            lbl2.Text = ToTable.TenBan;

            lbl1.Image = FromTable.GetSmallImage();
            lbl1.ForeColor = FromTable.GetTextColor();

            lbl2.Image = ToTable.GetSmallImage();
            lbl2.ForeColor = ToTable.GetTextColor();
        }

        private void GetData()
        {
            if (DataExchange != null)
            {
                LibExtend.Events.Sender obj = new Events.Sender(LibExtend.Events.ObjectName.DataSource, LibExtend.Events.ActionType.Get);
                obj.Item = this.FromTable;
                DataExchange(obj);

                if (!string.IsNullOrEmpty(obj.Error) && obj.Error.Length > 0)
                    MessageBox.Show(obj.Error, "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    DataTable DT = ((Table)obj.ObjectValue).DataSource;
                    if (DT != null && DT.Rows.Count > 0)
                    {
                        DT.Columns.Add(new DataColumn("Tick", Type.GetType("System.Boolean")));
                        foreach (DataRow ro in DT.Rows)
                            ro["Tick"] = true;
                    }
                    grid.DataSource = DT;
                }
            }
        }

        private bool AllowMove()
        {
            //Cả 2 bàn đều rỗng và chưa có thực đơn.
            if (ToTable.IdBanHang == 0 && FromTable.IdBanHang == 0)
                return false;
            //Cả 2 bàn đều đã thanh toán và chưa có bàn nào đc tạo mới => cả 2 bàn đều rỗng.
            if (ToTable.KhachTra > 0 && FromTable.KhachTra > 0)
                return false;
            //Cả 2 bàn đều đã có khách và đã phát sinh thực đơn, nhưng chưa có bàn nào thanh toán để trở thành 1 bàn rỗng.
            if (ToTable.IdBanHang > 0 && FromTable.IdBanHang > 0 && ToTable.KhachTra <= 0 && FromTable.KhachTra <= 0)
                return false;
            
            return true;
        }

        private bool AllowMerge()
        {
            if (ToTable.IdBanHang > 0 && ToTable.KhachTra <= 0 && FromTable.IdBanHang > 0 && FromTable.KhachTra <= 0)
                return true;
            //Cả 2 bàn đều rỗng và chưa có thực đơn.
            if (ToTable.IdBanHang == 0 && FromTable.IdBanHang == 0)
                return false;
            //Cả 2 bàn đều đã thanh toán và chưa có bàn nào đc tạo mới => cả 2 bàn đều rỗng.
            if (ToTable.KhachTra >= 0 && FromTable.KhachTra >= 0)
                return false;
            //ToTable rỗng
            if (ToTable.IdBanHang == 0 || ToTable.KhachTra >= 0 ||
                (ToTable.DataSource == null || ToTable.DataSource.Rows.Count == 0))
                return false;
            //FromTable rỗng
            if (FromTable.IdBanHang == 0 || FromTable.KhachTra >= 0 ||
                (FromTable.DataSource == null || FromTable.DataSource.Rows.Count == 0))
                return false;
            return true;
        }

        private void OptionFrom_Deactivate(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDoAction_Click(object sender, EventArgs e)
        {
            if (DataExchange != null)
            {
                LibExtend.Events.Sender obj = new Events.Sender(LibExtend.Events.ObjectName.mmo, LibExtend.Events.ActionType.Changed);
                LibExtend.Events.mmo mm = new Events.mmo();
                
                mm.MoveType = ToInt32(rdoGrp.EditValue);
                mm.MergeOption = ToInt32(rdGroupDetail.EditValue);
                mm.MenuOption = ToInt32(rdMoveSplit.EditValue);

                if (mm.MenuOption == 2)
                {
                    DataTable DT = (DataTable)grid.DataSource;
                    DataRow[] rs = DT.Select("Tick=1");
                    if (!object.Equals(rs, null) && rs.Length > 0)
                    {
                        if (rs.Length == DT.Rows.Count)
                        {
                            mm.MenuOption = 1;
                        }
                        else
                        {
                            DataTable dC = DT.Clone();
                            foreach (DataRow _r in rs)
                                dC.ImportRow(_r);
                            mm.DataSource = dC;
                        }
                    }
                }
                else //All
                    mm.DataSource = (DataTable)grid.DataSource;
                obj.mmo = mm;
                obj.Item = FromTable;
                obj.ToItem = ToTable;
                DataExchange(obj);

                if (!string.IsNullOrEmpty(obj.Error) && obj.Error.Length > 0)
                    MessageBox.Show(obj.Error, "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.Close();
            }
        }

        private int ToInt32(object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return 0;
            }
        }

        private void RdoEnable()
        {
            rdGroupDetail.Properties.Items[0].Enabled = (ToInt32(rdoGrp.EditValue) == 1) ? false : true;
            rdGroupDetail.Properties.Items[1].Enabled = rdGroupDetail.Properties.Items[0].Enabled;
            if (rdGroupDetail.Properties.Items[0].Enabled)
            {
                if (ToInt32(rdGroupDetail.EditValue) == 1)
                    rdGroupDetail_EditValueChanged(rdGroupDetail, new EventArgs());
            }
        }

        private void rdMoveSplit_EditValueChanged(object sender, EventArgs e)
        {
            grid.Enabled = (ToInt32(rdMoveSplit.EditValue) == 1) ? false : true;
        }

        private void rdGroupDetail_EditValueChanged(object sender, EventArgs e)
        {
            rdMoveSplit.Properties.Items[0].Enabled = (ToInt32(rdGroupDetail.EditValue) == 1) ? false : true;
            rdMoveSplit.Properties.Items[1].Enabled = rdMoveSplit.Properties.Items[0].Enabled;
        }
    }
}
